/**
 * 穿跨越结构计算算法模块
 * 提供桥梁跨越大腿结构设计的各项计算功能
 * 包含地基基础计算、不同类型桁架计算等核心算法
 */

/**
 * 穿跨越结构计算算法类
 * 实现地基基础、各类桁架等桥梁跨越结构的计算方法
 * 基于土力学和结构力学理论进行计算
 */
export class CrossingTrussAlgorithms {
  /**
   * 计算汉森承载力系数
   * 基于土的内摩擦角，使用汉森公式计算地基承载力的三个系数
   * 
   * @param {number} frictionAngle 土的内摩擦角 (度)
   * @returns {Object} 承载力系数对象
   * @returns {number} return.Nc - 承载力系数Nc
   * @returns {number} return.Nq - 承载力系数Nq
   * @returns {number} return.Ngamma - 承载力系数Ngamma
   */
  static calculateHansenCoefficients(frictionAngle: number) {
    // 将角度转换为弧度
    const phi = frictionAngle * Math.PI / 180;
    
    // 汉森公式承载力系数计算
    // Nq = exp(π·tanφ)·tan²(π/4 + φ/2)
    const Nq = Math.exp(Math.PI * Math.tan(phi)) * Math.pow(Math.tan(Math.PI/4 + phi/2), 2);
    
    // Nc = (Nq - 1) / tanφ
    const Nc = (Nq - 1) / Math.tan(phi);
    
    // Ngamma = 2·(Nq + 1)·tanφ
    const Ngamma = 2 * (Nq + 1) * Math.tan(phi);
    
    return { Nc, Nq, Ngamma };
  }
  
  /**
   * 地基基础计算
   * 基于汉森公式计算地基承载力，分析基础稳定性和受力情况
   * 考虑柱底弯矩、侧向荷载、安全系数等因素的综合影响
   * 
   * @param {number} foundationLength 基础长度 (m)
   * @param {number} foundationWidth 基础宽度 (m)
   * @param {number} foundationHeight 基础高度 (m)
   * @param {number} concreteDensity 钢筋混凝土密度 (kg/m³)
   * @param {number} safetyFactor 安全系数
   * @param {number} columnHeight 柱子高度 (m)
   * @param {number} foundationBuriedDepth 基础埋深 (m)
   * @param {number} soilUnitWeight 土重度 (kg/m³)
   * @param {number} foundationInterfaceCoefficient 基础界面系数
   * @param {number} trussLateralLoad 桁架侧向荷载 (kN)
   * @param {number} columnWidth 柱子宽度 (m)
   * @param {number} columnDepth 柱子深度 (m)
   * @param {number} columnElasticModulus 柱子弹性模量 (kPa)
   * @param {number} soilCohesion 土的粘聚力 (kPa)
   * @param {number} soilFrictionAngle 土的内摩擦角 (度)
   * @returns {Object} 地基基础计算结果对象
   * @returns {number} return.volume - 基础体积(m³)
   * @returns {number} return.weight - 基础重量(kg)
   * @returns {number} return.weightWithSafety - 考虑安全系数后的基础重量(kg)
   * @returns {number} return.surfaceArea - 基础表面积(m²)
   * @returns {number} return.foundationLoad - 基础荷载(kN)
   * @returns {number} return.foundationPressure - 基础压强(kPa)
   * @returns {number} return.bearingCapacity - 地基允许承载力(kPa)
   * @returns {number} return.ultimateBearingCapacity - 地基极限承载力(kPa)
   * @returns {boolean} return.safetyCheck - 安全性检查结果
   * @returns {number} return.columnHeight - 柱子高度(m)
   * @returns {number} return.foundationBuriedDepth - 基础埋深(m)
   * @returns {number} return.soilWeight - 土重(kN)
   * @returns {number} return.totalLoad - 总荷载(kN)
   * @returns {number} return.foundationInterfaceCoefficient - 基础界面系数
   * @returns {number} return.foundationCrossSectionArea - 基础截面面积(m²)
   * @returns {number} return.trussLateralLoad - 桁架侧向荷载(kN)
   * @returns {number} return.columnBottomMoment - 柱底弯矩(kN·m)
   * @returns {number} return.momentEffect - 弯矩影响(kPa)
   * @returns {number} return.columnLateralStiffness - 柱抗侧刚度(kN/m)
   * @returns {number} return.columnShearForce - 柱水平剪力(kN)
   */
  static foundationCalculation(
    foundationLength: number,
    foundationWidth: number,
    foundationHeight: number,
    concreteDensity: number,
    safetyFactor: number,
    columnHeight: number,
    foundationBuriedDepth: number,
    soilUnitWeight: number,
    foundationInterfaceCoefficient: number,
    trussLateralLoad: number,
    columnWidth: number,
    columnDepth: number,
    columnElasticModulus: number,
    soilCohesion: number,
    soilFrictionAngle: number,
  ): {
    volume: number;
    weight: number;
    weightWithSafety: number;
    surfaceArea: number;
    foundationLoad: number;
    foundationPressure: number;
    bearingCapacity: number;
    ultimateBearingCapacity: number;
    safetyCheck: boolean;
    columnHeight: number;
    foundationBuriedDepth: number;
    soilWeight: number;
    totalLoad: number;
    foundationInterfaceCoefficient: number;
    foundationCrossSectionArea: number;
    trussLateralLoad: number;
    columnBottomMoment: number;
    momentEffect: number;
    columnLateralStiffness: number;
    columnShearForce: number;
  } {
    // 计算矩形截面柱子的惯性矩 I = (宽度 * 深度^3) / 12
    const columnInertiaMoment = (columnWidth * Math.pow(columnDepth, 3)) / 12;
    
    // 计算基础截面面积
    const foundationCrossSectionArea = foundationLength * foundationWidth;

    // 计算基础体积（单位：m³）
    const volume = foundationLength * foundationWidth * foundationHeight;

    // 计算基础重量（单位：kg）
    const weight = volume * concreteDensity;
    
    // 考虑安全系数后的基础重量
    const weightWithSafety = weight * safetyFactor;

    // 计算基础表面积（单位：m²）
    const topSurfaceArea = foundationLength * foundationWidth;
    const bottomSurfaceArea = foundationLength * foundationWidth;
    const sideSurfaceArea = 2 * (foundationLength * foundationHeight + foundationWidth * foundationHeight);
    const totalSurfaceArea = topSurfaceArea + bottomSurfaceArea + sideSurfaceArea;

    // 计算土重（基础埋深部分体积 * 土重度 * 重力加速度）
    const soilVolume = foundationCrossSectionArea * foundationBuriedDepth;
    const soilWeight = soilVolume * soilUnitWeight * 9.81 / 1000; // 转换为 kN

    // 使用汉森公式计算地基极限承载力
    // 1. 计算承载力系数
    const { Nc, Nq, Ngamma } = this.calculateHansenCoefficients(soilFrictionAngle);
    
    // 2. 计算超载项 q = γ * D (kPa)
    const overburdenPressure = (soilUnitWeight * 9.81 * foundationBuriedDepth) / 1000;
    
    // 3. 汉森公式计算极限承载力 (kPa)
    // qu = c*Nc + q*Nq + 0.5*γ*B*Ngamma
    const ultimateBearingCapacity = soilCohesion * Nc + 
                                   overburdenPressure * Nq + 
                                   0.5 * (soilUnitWeight * 9.81 / 1000) * foundationWidth * Ngamma;
                                    
    // 计算地基允许承载力 (kPa) - 考虑安全系数
    const bearingCapacity = ultimateBearingCapacity / safetyFactor;
    
    // 计算基础荷载（基础重量 + 土重）
    const foundationLoad = (weightWithSafety * 9.81 / 1000) + soilWeight; // kN
    
    // 计算基础压强，避免除零错误
    const foundationPressure = topSurfaceArea !== 0 ? foundationLoad / topSurfaceArea : 0; // kPa

    // 计算总荷载
    const totalLoad = foundationLoad + (columnHeight * foundationInterfaceCoefficient);
    
    // 计算柱底弯矩（桁架侧向荷载 × 柱子高度）
    const columnBottomMoment = trussLateralLoad * columnHeight;
    
    // 计算弯矩影响（简化计算，假设弯矩在基础底部产生线性分布应力）
    // 公式：momentEffect = M * 6 / (B * L^2)
    // 其中 M 为柱底弯矩，B 为基础宽度，L 为基础长度
    const momentEffect = (foundationLength !== 0 && foundationWidth !== 0) ? 
      (columnBottomMoment * 6) / (foundationWidth * Math.pow(foundationLength, 2)) : 0;
    
    // 计算柱抗侧刚度 (公式：12 * E * I / h³)
    // 添加安全检查防止除零错误
    const columnLateralStiffness = columnHeight !== 0 ? 
      (12 * columnElasticModulus * columnInertiaMoment) / Math.pow(columnHeight, 3) : 0;
    
    // 计算单柱水平剪力（等于桁架侧向荷载）
    const columnShearForce = trussLateralLoad;

    return {
      volume: parseFloat(volume.toFixed(3)),
      weight: parseFloat(weight.toFixed(2)),
      weightWithSafety: parseFloat(weightWithSafety.toFixed(2)),
      surfaceArea: parseFloat(totalSurfaceArea.toFixed(2)),
      foundationLoad: parseFloat(foundationLoad.toFixed(2)),
      foundationPressure: parseFloat(foundationPressure.toFixed(2)),
      ultimateBearingCapacity: parseFloat(ultimateBearingCapacity.toFixed(2)),
      bearingCapacity: parseFloat(bearingCapacity.toFixed(2)),
      safetyCheck: (foundationPressure + Math.abs(momentEffect)) < bearingCapacity,
      columnHeight: parseFloat(columnHeight.toFixed(2)),
      foundationBuriedDepth: parseFloat(foundationBuriedDepth.toFixed(2)),
      soilWeight: parseFloat(soilWeight.toFixed(2)),
      totalLoad: parseFloat(totalLoad.toFixed(2)),
      foundationInterfaceCoefficient: parseFloat(foundationInterfaceCoefficient.toFixed(2)),
      foundationCrossSectionArea: parseFloat(foundationCrossSectionArea.toFixed(2)),
      trussLateralLoad: parseFloat(trussLateralLoad.toFixed(2)),
      columnBottomMoment: parseFloat(columnBottomMoment.toFixed(2)),
      momentEffect: parseFloat(momentEffect.toFixed(2)),
      columnLateralStiffness: parseFloat(columnLateralStiffness.toFixed(2)),
      columnShearForce: parseFloat(columnShearForce.toFixed(2))
    };
  }

  /**
   * 桁架基本参数计算
   * 计算桁架的材料用量、重量、线荷载等基本参数
   * 考虑材料密度和腐蚀系数的影响
   * 
   * @param {number} length 桁架长度 (m)
   * @param {number} height 桁架高度 (m)
   * @param {number} width 桁架宽度 (m)
   * @param {number} density 材料密度 (kg/m³)
   * @param {number} corrosionFactor 腐蚀系数
   * @returns {Object} 桁架计算结果对象
   * @returns {number} return.materialVolume - 材料体积(m³)
   * @returns {number} return.weight - 重量(kg)
   * @returns {number} return.weightWithCorrosion - 考虑腐蚀后的重量(kg)
   * @returns {number} return.linearLoad - 线荷载(N/m)
   * @returns {number} return.supportReaction - 支反力(N)
   */
  static trussCalculation(
    length: number,
    height: number,
    width: number,
    density: number,
    corrosionFactor: number
  ): {
    materialVolume: number;
    weight: number;
    weightWithCorrosion: number;
    linearLoad: number;
    supportReaction: number;
  } {
    // 计算桁架包络体积
    const envelopeVolume = length * height * width;
    
    // 使用简化模型计算材料体积，假设材料占包络体积的20%
    const materialVolume = envelopeVolume * 0.2;

    // 计算重量（体积 × 密度）
    const weight = materialVolume * density;
    
    // 考虑腐蚀系数后的重量
    const weightWithCorrosion = weight * corrosionFactor;

    // 计算线荷载（总重量 × 重力加速度 ÷ 长度）
    const linearLoad = (weightWithCorrosion * 9.81) / length;

    // 计算支反力（简支梁，均布荷载下支反力为总荷载的一半）
    const supportReaction = linearLoad * length / 2;

    return {
      materialVolume: parseFloat(materialVolume.toFixed(3)),
      weight: parseFloat(weight.toFixed(2)),
      weightWithCorrosion: parseFloat(weightWithCorrosion.toFixed(2)),
      linearLoad: parseFloat(linearLoad.toFixed(2)),
      supportReaction: parseFloat(supportReaction.toFixed(2))
    };
  }

  /**
   * 正三角桁架结构计算
   * 计算正三角形截面桁架的内力、弯矩和挠度
   * 基于结构力学原理分析桁架各杆件受力情况
   * 
   * @param {number} length 桁架长度 (m)
   * @param {number} height 桁架高度 (m)
   * @param {number} load 外加载荷 (N)
   * @returns {Object} 正三角桁架计算结果对象
   * @returns {number} return.reaction - 支座反力(N)
   * @returns {number} return.topChordForce - 顶部横杆内力(N)
   * @returns {number} return.topChordMoment - 顶部横杆弯矩(N·m)
   * @returns {number} return.bottomChordForce - 底部横杆内力(N)
   * @returns {number} return.bottomChordMoment - 底部横杆弯矩(N·m)
   * @returns {number} return.deflection - 挠度(m)
   */
  static triangularTrussCalculation(
    length: number,
    height: number,
    load: number
  ): {
    reaction: number;
    topChordForce: number;
    topChordMoment: number;
    bottomChordForce: number;
    bottomChordMoment: number;
    deflection: number;
  } {
    // 计算支座反力（对称荷载，每个支座承受一半荷载）
    const reaction = load / 2;

    // 计算几何参数
    const halfLength = length / 2;
    const hypotenuse = Math.sqrt(Math.pow(halfLength, 2) + Math.pow(height, 2)); // 斜杆长度
    const cosTheta = halfLength / hypotenuse; // 余弦值
    const sinTheta = height / hypotenuse;     // 正弦值

    // 计算顶部横杆内力
    // 顶部横杆主要承受压力
    const topChordForce = reaction / sinTheta * cosTheta;
    
    // 计算顶部横杆弯矩（简化计算）
    const topChordMoment = (load * length) / 16;

    // 计算底部横杆内力
    // 底部横杆主要承受拉力
    const bottomChordForce = topChordForce;
    
    // 计算底部横杆弯矩（简化计算）
    const bottomChordMoment = (load * length) / 12;

    // 计算挠度
    // 假设弹性模量E = 210 GPa（钢材）
    const E = 210 * Math.pow(10, 9); // 弹性模量
    
    // 假设惯性矩I（假设宽度为1.0m）
    const I = (1.0 * Math.pow(height, 3)) / 12;
    
    // 简支梁跨中集中荷载下的挠度公式: δ = (F * L^3) / (48 * E * I)
    const deflection = (load * Math.pow(length, 3)) / (48 * E * I);

    return {
      reaction: parseFloat(reaction.toFixed(2)),
      topChordForce: parseFloat(topChordForce.toFixed(2)),
      topChordMoment: parseFloat(topChordMoment.toFixed(2)),
      bottomChordForce: parseFloat(bottomChordForce.toFixed(2)),
      bottomChordMoment: parseFloat(bottomChordMoment.toFixed(2)),
      deflection: parseFloat(deflection.toExponential(2))
    };
  }

  /**
   * 矩形桁架结构计算
   * 计算矩形截面桁架的内力、弯矩和挠度
   * 基于结构力学原理分析矩形桁架的受力特性
   * 
   * @param {number} length 桁架长度 (m)
   * @param {number} height 桁架高度 (m)
   * @param {number} width 桁架宽度 (m)
   * @param {number} load 外加载荷 (N)
   * @returns {Object} 矩形桁架计算结果对象
   * @returns {number} return.reaction - 支座反力(N)
   * @returns {number} return.topChordForce - 顶部横杆内力(N)
   * @returns {number} return.topChordMoment - 顶部横杆弯矩(N·m)
   * @returns {number} return.bottomChordForce - 底部横杆内力(N)
   * @returns {number} return.bottomChordMoment - 底部横杆弯矩(N·m)
   * @returns {number} return.deflection - 挠度(m)
   */
  static rectangularTrussCalculation(
    length: number,
    height: number,
    width: number,
    load: number
  ): {
    reaction: number;
    topChordForce: number;
    topChordMoment: number;
    bottomChordForce: number;
    bottomChordMoment: number;
    deflection: number;
  } {
    // 计算支座反力（对称荷载，每个支座承受一半荷载）
    const reaction = load / 2;

    // 计算顶部横杆内力
    // 顶部横杆主要承受压力
    const topChordForce = reaction;
    
    // 计算顶部横杆弯矩（简化计算）
    const topChordMoment = (load * length) / 15;

    // 计算底部横杆内力
    // 底部横杆主要承受拉力
    const bottomChordForce = reaction;
    
    // 计算底部横杆弯矩（简化计算）
    const bottomChordMoment = (load * length) / 10;

    // 计算挠度
    // 假设弹性模量E = 210 GPa（钢材）
    const E = 210 * Math.pow(10, 9); // 弹性模量
    
    // 计算惯性矩I
    const I = (width * Math.pow(height, 3)) / 12;
    
    // 简支梁跨中集中荷载下的挠度公式: δ = (F * L^3) / (48 * E * I)
    const deflection = (load * Math.pow(length, 3)) / (48 * E * I);

    return {
      reaction: parseFloat(reaction.toFixed(2)),
      topChordForce: parseFloat(topChordForce.toFixed(2)),
      topChordMoment: parseFloat(topChordMoment.toFixed(2)),
      bottomChordForce: parseFloat(bottomChordForce.toFixed(2)),
      bottomChordMoment: parseFloat(bottomChordMoment.toFixed(2)),
      deflection: parseFloat(deflection.toExponential(2))
    };
  }

  /**
   * 倒三角桁架结构计算
   * 计算倒三角形截面桁架的内力、弯矩和挠度
   * 基于结构力学原理分析倒三角桁架的受力特性
   * 
   * @param {number} length 桁架长度 (m)
   * @param {number} height 桁架高度 (m)
   * @param {number} width 桁架宽度 (m)
   * @param {number} load 外加载荷 (N)
   * @returns {Object} 倒三角桁架计算结果对象
   * @returns {number} return.reaction - 支座反力(N)
   * @returns {number} return.topChordForce - 顶部横杆内力(N)
   * @returns {number} return.topChordMoment - 顶部横杆弯矩(N·m)
   * @returns {number} return.bottomChordForce - 底部横杆内力(N)
   * @returns {number} return.bottomChordMoment - 底部横杆弯矩(N·m)
   * @returns {number} return.deflection - 挠度(m)
   */
  static invertedTriangularTrussCalculation(
    length: number,
    height: number,
    width: number,
    load: number
  ): {
    reaction: number;
    topChordForce: number;
    topChordMoment: number;
    bottomChordForce: number;
    bottomChordMoment: number;
    deflection: number;
  } {
    // 计算支座反力（对称荷载，每个支座承受一半荷载）
    const reaction = load / 2;

    // 计算几何参数
    const halfLength = length / 2;
    const hypotenuse = Math.sqrt(Math.pow(halfLength, 2) + Math.pow(height, 2)); // 斜杆长度
    const cosTheta = halfLength / hypotenuse; // 余弦值
    const sinTheta = height / hypotenuse;     // 正弦值

    // 计算顶部横杆（两根）内力
    // 顶部横杆主要承受压力
    const topChordForce = reaction / sinTheta * cosTheta;
    
    // 计算顶部横杆弯矩（简化计算）
    const topChordMoment = (load * length) / 16;

    // 计算底部横杆内力
    // 底部横杆主要承受拉力
    const bottomChordForce = topChordForce;
    
    // 计算底部横杆弯矩（简化计算）
    const bottomChordMoment = (load * length) / 12;

    // 计算挠度
    // 假设弹性模量E = 210 GPa（钢材）
    const E = 210 * Math.pow(10, 9); // 弹性模量
    
    // 计算惯性矩I
    const I = (width * Math.pow(height, 3)) / 12;
    
    // 简支梁跨中集中荷载下的挠度公式: δ = (F * L^3) / (48 * E * I)
    const deflection = (load * Math.pow(length, 3)) / (48 * E * I);

    return {
      reaction: parseFloat(reaction.toFixed(2)),
      topChordForce: parseFloat(topChordForce.toFixed(2)),
      topChordMoment: parseFloat(topChordMoment.toFixed(2)),
      bottomChordForce: parseFloat(bottomChordForce.toFixed(2)),
      bottomChordMoment: parseFloat(bottomChordMoment.toFixed(2)),
      deflection: parseFloat(deflection.toExponential(2))
    };
  }
}